﻿using MediatR;
using WMS.Api.Application.Commands.Users;
using WMS.Common;
using WMS.Domain;
using WMS.Infrastructure;

namespace WMS.Api.Application.CommandHandlers.Users
{
    public class GetUserCommandHandler : IRequestHandler<GetUserCommand, ApiResult<User>>
    {
        private readonly IBaseRepository<User> _userRepo;

        public GetUserCommandHandler(IBaseRepository<User> userRepo)
        {
            _userRepo = userRepo;
        }

        public async Task<ApiResult<User>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<User> result = new ApiResult<User>("获取用户");

            var user = await _userRepo.GetValueByIdAsync(request.Id);

            if (user == null)
            {
                result.isSuc = false;
                result.data = null;
                result.msg = "用户不存在";
                result.code = StatusCodeEnum.Fail;
                return result;
            }

            result.data = user;
            result.isSuc = true;
            result.msg = "获取成功";
            result.code = StatusCodeEnum.Ok;

            return result;
        }
    }
}
